//************************************************************
//  Copyright (c)
//	FileName	  : main.c
//	Function	  : Main Function
//  Instructions  : Contains the MCU initialization function and its H file
//************************************************************
/********************Includes************************************************************************/
#include "SC_Init.h" // MCU initialization header file, including all firmware library header files
#include <stdio.h>
#include "SC_it.h"
#include "..\Drivers\SCDriver_list.h"
#include "tm1668.h"
#include "drv_sonsor_air.h"
#include "drv_rgb.h"
#include "SMProcess.h"
#include "stdio.h"
#include "toolkit.h"
#include "sc95f_iap.h"
#include "filterCounter.h"
#include "drv_key.h"
//#include "host_cmd.h"
//#include "ringbuffer.h"
#include "filterCounter.h"

#include "protol_iot_mj.h"
/**************************************Generated by EasyCodeCube*************************************/
// Forbid editing areas between the labels !!!

/*************************************.Generated by EasyCodeCube.************************************/
uint32_t xdata eventValue = 0;
uint32_t xdata mainTick = 0;
extern bit UartXReceiveFlag;
uint32_t xdata countDownTime = 0;
uint32_t xdata countDownFlag = 0;

uint8_t BTMCount_1S = 0;
uint32_t xdata BTMCount_1min = 0;

#define reduceLightTime  3  //30
bool reduceLightEvt = FALSE;
bool reduceLightFlag = FALSE;
bool reducelightStep = FALSE;
uint8_t reduceLight_1S = 0;
uint32_t xdata reduceLightCount_1min = 0;
extern bit SecondFlag;

extern volatile bool filter_write_flag;
extern void filter_count_handle(void);
extern bool fileter_ONOFF(void);

//static uint8_t wifi_buffer_size = 64;
#define wifi_buffer_size (uint8_t)64
static uint8_t wifi_buffer[wifi_buffer_size] = {0};

/*****************************************************************************************************
 * Function Name: main
 * Description  : This function implements main function.
 * Arguments    : None
 * Return Value : None
 ******************************************************************************************************/
void uart0SendByte(uint8_t Data)
{
	US0CON3 = Data;
	while (!(US0CON0 & 0x02))
		;
	// USCI0_ClearFlag(USCI0_UART_FLAG_TI);
	// US0CON0 = US0CON0  & 0xFC | 0x02;
	US0CON0 &= (~0x02);
	// return c;
}
                                                                                                                                                                                                                                                                                                                       
void usart0_transmit_datas(unsigned char *sendBuffer, unsigned int sendSize)
{
	uint32_t count = sendSize;
	while (count > 0)
	{
		uart0SendByte(sendBuffer[sendSize - count]);
		count--;
	}
}

void enterLowPowerMode(void)
{
	PCON |= 0X02;
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
}

void standbyTimingHandle(void)
{
	if (SecondFlag)
	{
		if (fileter_ONOFF() == TRUE)
		{

			++BTMCount_1S;
			if (BTMCount_1S >= 60)
			{
				++BTMCount_1min;
				BTMCount_1S = 0;
				if (BTMCount_1min >= intervalWriteTime) //
				{
					filter_write_flag = TRUE;
				}
			}
		}
		SecondFlag = 0;
	}
}

void Version_Print(void)
{
//	printf("\r\n-------------------------------\r\n");
//	printf("		FJAOE F20S    \r\n");
//	printf("		soft version:%s\r\n", SOFTWARE_VERSION_F20);
//	printf("		relese date:%s %s\r\n", __DATE__, __TIME__);
//	printf("-----------------------------------\r\n");
}


uint16_t xdata i_main = 0;
void main(void)
{
	SC_Init();
	Version_Print();
	TouchKeyInit();
	tm1668Init();
    //middleWareInit();
	WDT_Init(WDT_OverflowTime_500MS);
	WDT_Cmd(ENABLE);
	//iot_model_init();
	while (1)
	{
		/*input*/
		sensor_air_data_handle();	// 1
		touchKeyScanHandle();		// 2
		//iot_model_recv_handle();
		//host_cmd_data_handle();		// 3
		standbyTimingHandle();		// 4   
		filter_count_handle(); //2
		/*process*/
		DeviceProcess(&eventValue);	//1
//		
		eventValue = 0;
//		/*output*/
		beep_control_handle();		//1
		tm1668_led_flush_handle();	//2
		WDT_SetReload();
	}
}